﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;

namespace HomeMediaBrowser
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:HomeMediaBrowser"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:HomeMediaBrowser;assembly=HomeMediaBrowser"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:TabBorder/>
    ///
    /// </summary>
    public partial class TabBorder : Decorator
    {
        static TabBorder()
        {
         
        }
        public static readonly DependencyProperty ThumbnailProperty =
            DependencyProperty.Register("Thumbnail", typeof(ImageSource), typeof(TabBorder), new FrameworkPropertyMetadata());
        public ImageSource Thumbnail
        {
            get { return (ImageSource)GetValue(ThumbnailProperty); }
            set { SetValue(ThumbnailProperty, value); }
        }
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TabBorder), new FrameworkPropertyMetadata());
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(Boolean), typeof(TabBorder), new FrameworkPropertyMetadata());
        public Boolean IsSelected
        {
            get { return (Boolean)GetValue(IsSelectedProperty); }
            set {
                Boolean OldSelect = IsSelected;
                SetValue(IsSelectedProperty, value);
                if (OldSelect != value)
                    InvalidateVisual();
            }
        }

        public static RoutedEvent CloseEvent = EventManager.RegisterRoutedEvent("CloseEvent", RoutingStrategy.Bubble,
                                        typeof(RoutedEventHandler), typeof(TabBorder));
        public static RoutedEvent SelectEvent = EventManager.RegisterRoutedEvent("SelectEvent", RoutingStrategy.Bubble,
                                        typeof(RoutedEventHandler), typeof(TabBorder));

        public virtual void OnClose(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs();
            args.RoutedEvent = CloseEvent;
            args.Source = this;
            RaiseEvent(args);
        }
        public virtual void OnSelect(object sender, RoutedEventArgs e)
        {
            MouseButtonEventArgs me = e as MouseButtonEventArgs;
            if (me == null)
                return;
            Point pt = me.GetPosition(this);
            if (!IsPtIn(pt))
                return;
            RoutedEventArgs args = new RoutedEventArgs();
            args.RoutedEvent = SelectEvent;
            args.Source = this;
            RaiseEvent(args);
        }
        public TabBorder()
        {
            InitializeComponent();
            MouseEnter +=new MouseEventHandler(TabBorder_MouseEnter);
            MouseLeave += new MouseEventHandler(TabBorder_MouseEnter);
        }
        public void TabBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            InvalidateVisual();
        }
        public void HideTab()
        {
            MaxWidth = 0;
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            Point pt = e.GetPosition(this);
            //
            if (IsPtIn(pt))
                base.OnMouseLeftButtonDown(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            //
            if (IsPtIn(pt))
                base.OnMouseMove(e);
        }
        bool IsPtIn(Point pt)
        {
            return true;
        }
        bool IsPtIn_20100311(Point pt)
        {
            if (IsPtLeft(pt))
                return false;
            pt.X = ActualWidth - pt.X;
            if (IsPtLeft(pt))
                return false;
            return true;
        }
        bool IsPtLeft(Point pt)
        {
            if (pt.X >= 0 && pt.X <= 10 &&
                pt.Y >= 0 && pt.Y <= 24)
                return true;
            return false;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            return base.ArrangeOverride(finalSize);
        }
        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(constraint);
        }
        protected void OnRender_20100311(DrawingContext drawingContext)
        {
            //base.OnRender(drawingContext);
            double BtnWidth = ActualWidth;
            double BtnHeight = ActualHeight;
            if (BtnWidth <= 0)
                return;
            //drawingContext.DrawRectangle(new SolidColorBrush(Colors.Black),new Pen(new SolidColorBrush(Colors.Blue),3),new Rect(0,0,BtnWidth,BtnHeight));
            PathGeometry GeoBorder = new PathGeometry();
            PathFigure Figure = new PathFigure();
            Figure.IsClosed = false;
            Figure.IsFilled = false;

            Figure.Segments = new PathSegmentCollection();
            Figure.StartPoint = new Point(0, 24);
            //StartPoint:0,100 Control Point1: 45 100 Control Point2:45 0 EndPoint:100,0
            BezierSegment Left = new BezierSegment(new Point(7, 22), new Point(3.5, 2), new Point(10, 1), true);
            Figure.Segments.Add(Left);
            GeoBorder.Figures.Add(Figure);

            LineSegment Middle = new LineSegment(new Point(BtnWidth - 10, 1), true);
            Figure.Segments.Add(Middle);
            double offset = BtnWidth;

            BezierSegment Right = new BezierSegment(new Point(offset - 3, 2), new Point(offset - 6.5, 22), new Point(offset, 24), true);
            Figure.Segments.Add(Right);

            GeoBorder.Figures.Add(Figure);

            BitmapEffect BE = (BitmapEffect)this.Resources["BorderBitmapEffect"];
            drawingContext.PushEffect(BE, null);
            Color C = Color.FromArgb(60, 0, 0, 0);
            
            drawingContext.DrawGeometry(new SolidColorBrush(Colors.Transparent), new Pen(new SolidColorBrush(C), 1), GeoBorder);
            drawingContext.Pop();
            //Figure.IsClosed = true;
            Figure.IsFilled = true;
            GeoBorder.FillRule = FillRule.Nonzero;
            Color BK = Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF);
            if (IsSelected)
                BK = Color.FromArgb(0x99, 0xFF, 0xFF, 0xFF);
            else if (IsMouseOver)
                BK = Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF);
            drawingContext.DrawGeometry(new SolidColorBrush(BK), new Pen(new SolidColorBrush(Colors.Red), 0), GeoBorder);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            //base.OnRender(drawingContext);
            double BtnWidth = ActualWidth;
            double BtnHeight = ActualHeight;
            if (BtnWidth <= 0)
                return;
            //drawingContext.DrawRectangle(new SolidColorBrush(Colors.Black),new Pen(new SolidColorBrush(Colors.Blue),3),new Rect(0,0,BtnWidth,BtnHeight));
            PathGeometry GeoBorder = new PathGeometry();
            PathFigure Figure = new PathFigure();
            Figure.IsClosed = false;
            Figure.IsFilled = false;

            Figure.Segments = new PathSegmentCollection();
            Figure.StartPoint = new Point(0, 25);
           
            ArcSegment Left1 = new ArcSegment(new Point(4, 25-4), new Size(4,4),0, false, SweepDirection.Counterclockwise, true);
            LineSegment Left2 = new LineSegment(new Point(4, 4), true);
            ArcSegment Left3 = new ArcSegment(new Point(8, 0), new Size(4,4), 0, false, SweepDirection.Clockwise, true);
            Figure.Segments.Add(Left1);
            Figure.Segments.Add(Left2);
            Figure.Segments.Add(Left3);
            GeoBorder.Figures.Add(Figure);

            LineSegment Middle = new LineSegment(new Point(BtnWidth - 10, 1), true);
            Figure.Segments.Add(Middle);
            double offset = BtnWidth;

            //BezierSegment Right = new BezierSegment(new Point(offset - 3, 2), new Point(offset - 3, 22), new Point(offset, 24), true);
            ArcSegment Right1 = new ArcSegment(new Point(offset - 4, 4), new Size(4, 4), 0, false, SweepDirection.Clockwise, true);
            LineSegment Right2 = new LineSegment(new Point(offset - 4, 25 - 4), true);
            ArcSegment Right3 = new ArcSegment(new Point(offset, 25), new Size(4, 4), 0, false, SweepDirection.Counterclockwise, true);
            Figure.Segments.Add(Right1);
            Figure.Segments.Add(Right2);
            Figure.Segments.Add(Right3);

            GeoBorder.Figures.Add(Figure);

            BitmapEffect BE = (BitmapEffect)this.Resources["BorderBitmapEffect"];
            drawingContext.PushEffect(BE, null);
            Color C = Color.FromArgb(0x3C, 0, 0, 0);

            drawingContext.DrawGeometry(new SolidColorBrush(Colors.Transparent), new Pen(new SolidColorBrush(C), 1), GeoBorder);
            drawingContext.Pop();

            //Figure.IsClosed = true;
            Figure.IsFilled = true;
            GeoBorder.FillRule = FillRule.Nonzero;
            Color BK = Color.FromArgb(0x0, 0xFF, 0xFF, 0xFF);
            if (IsSelected)
                BK = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
            else if (IsMouseOver)
                BK = Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF);

            drawingContext.DrawGeometry(new SolidColorBrush(BK), null, GeoBorder);
        }
    }
}
